<?php

namespace app\model;

//use support\Model;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaPurchasedVehiclesRelevance
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_purchased_vehicles_relevance';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;


    /**
     * @param $page
     * @param $pageNum
     * @param $purchasecontractsn
     * @param string[] $fields
     * @param array $condition
     * @param array $between
     * @return false|string
     */
    public static function list($page, $pageNum,$fields = ['*'], $condition = [], $between = []){
        $offset = ($page - 1) * $pageNum;
            if ($between) {
                $model = Db::table('wa_purchased_vehicles_relevance')
                    ->select($fields)
                    ->where($condition)
                    ->whereBetween('created_at', $between);
            } else {
                $model = Db::table('wa_purchased_vehicles_relevance')
                    ->select($fields)
                    ->where($condition);
            }


        $count = $model->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
        if ($list){
            foreach ($list as $k){

            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }

    /**
     * @param $data
     * @return int
     */
    public static function find($condition)
    {
        $res = Db::table('wa_purchased_vehicles_relevance')
            ->where($condition)
            ->first();
        return json_encode($res);
    }

    /**
     * @param $data
     * @return int
     */
    public static function created($data)
    {
        return Db::table('wa_purchased_vehicles_relevance')->insertGetId($data);
    }


    public static function all($condition)
    {
        $list = Db::table('wa_purchased_vehicles_relevance')
            ->select('*')
            ->where($condition)
            ->get();

        return json_encode($list);
    }


}